//
//  WBEmoticonKeyBoardCell.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/14.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit

class WBEmoticonKeyBoardCell: UICollectionViewCell {
    
    var indexPath:IndexPath? {
        
        didSet {
            
            if let currIndexPath = indexPath {
                
                messageLabel.text = "第\(currIndexPath.section)组第\(currIndexPath.item)个"
                
            }
            
        }
    }
    // MARK: --删除按钮
    fileprivate lazy var deleteButton:UIButton = {
        
        let button = UIButton()
        button.setImage(UIImage(named:"compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named:"compose_emotion_delete_highlighted"), for: .highlighted)
        
        button.addTarget(self, action: #selector(clickDeleteButton), for: .touchUpInside)
        
        return button
    }()
    
    private lazy var buttonArray:[WBEmoticonButton] = [WBEmoticonButton]()
    
    var emoticonArray: [WBEmoticon]?{
    
        didSet {
            
            if let currEmoticonArray = emoticonArray {
                
                for button in buttonArray {
                    
                    button.isHidden = true
                    
                }
                
                
                for (i,emoticon) in currEmoticonArray.enumerated() {
                    
                    let button = buttonArray[i]
                    
                    button.isHidden = false
                    
                    button.emoticon = emoticon
                                        
                }
                
            }
        }
        
    }
    
    fileprivate lazy var messageLabel: UILabel = {
        
        let label = UILabel(fontSize: 20, textColor: UIColor.red)
        
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        addChildButton()
        
//        contentView.addSubview(messageLabel)
//        
//        messageLabel.snp.makeConstraints { (make) in
//            
//            make.center.equalTo(contentView)
//        }
    }
    
    // MARK: --添加表情按钮的方法
    private func addChildButton() {
        
        // 宽度
        let buttonWidth = self.frame.size.width / 7
        // 高度
        let buttonHeight =  self.frame.size.height / 3
        
        for i in 0..<20 {
            
            let button = WBEmoticonButton()
            // 列的索引
            let colIndex = i % 7
            // 行的索引
            let rowIndex = i / 7
            button.frame.origin.x = CGFloat(colIndex) * buttonWidth
            button.frame.origin.y = CGFloat(rowIndex) * buttonHeight
            button.frame.size = CGSize(width: buttonWidth, height: buttonHeight)
                        
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            
            self.buttonArray.append(button)
            
            button.addTarget(self, action: #selector(clickButton(btn:)), for: .touchUpInside)
            
            contentView.addSubview(button)
            
        }
        
        contentView.addSubview(deleteButton)
        
        deleteButton.frame = CGRect(x: self.frame.size.width - buttonWidth, y: self.frame.size.height - buttonHeight, width: buttonWidth, height: buttonHeight)
        
    }
    // MARK: --点击删除按钮
    @objc private func clickDeleteButton() {
        
        NotificationCenter.default.post(name: NSNotification.Name(ClickDeleteButtonNotificationName), object: nil)
        
    }
    
    // MARK: --点击表情按钮
    @objc private func clickButton(btn:WBEmoticonButton) {
        
        NotificationCenter.default.post(name: NSNotification.Name(ClickEmotionButtonNotificationName), object: btn.emoticon!)
        
    }
}
